/*
 * Copyright (c) 2022-2022 All rights Reserved.
 */

package com.driving.core.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.driving.core.base.BaseController;
import com.driving.core.base.IPersonController;
import com.driving.core.entity.Diary;
import com.driving.core.entity.Student;
import com.driving.core.mapper.type.Gender;
import com.driving.core.service.IDiaryService;
import com.driving.core.service.IStudentService;
import com.driving.core.service.dto.ApplyDTO;
import com.driving.core.service.dto.StudentDTO;
import com.driving.core.service.dto.SuggestDTO;
import com.driving.core.util.Constant;
import com.driving.core.util.Constant.Audience;
import com.driving.core.util.JsonWebToken;
import com.driving.core.util.MessageType.ExceptionType;
import com.driving.core.util.Response;

import lombok.AllArgsConstructor;

/**
 * 学员接口控制器
 *
 * @author SAGITTAR
 * @since 2017
 */
@AllArgsConstructor
@RestController
@RequestMapping(value = "/student")
public class StudentController extends BaseController<IStudentService, Student>
        implements IPersonController<StudentDTO> {

    private final IDiaryService diaryService;

    @Override
    @PostMapping(value = "/password-update")
    public Response<Boolean> passwordUpdate(HttpServletRequest request, @RequestBody StudentDTO studentDTO) {
        if (!validateAllParams(studentDTO.getOldPassword(), studentDTO.getPassword())) {
            return Response.failure(configReader.getExceptionMessage(ExceptionType.NOT_COMPLETE));
        }
        String token = request.getHeader(Constant.TOKEN);
        return Response.success(baseService.passwordUpdateOrReset(studentDTO, token));
    }

    @Override
    @PostMapping(value = "/password-reset")
    public Response<Boolean> passwordReset(@RequestBody StudentDTO dto) {
        if (!validateAllParams(dto.getMobile(), dto.getPassword())) {
            return Response.failure(configReader.getExceptionMessage(ExceptionType.NOT_COMPLETE));
        }
        return Response.success(baseService.passwordUpdateOrReset(dto, null));
    }

    @PostMapping(value = "/register")
    public Response<Map<String, Object>> register(@RequestBody StudentDTO studentDTO) {
        String name = studentDTO.getNickName();
        Gender gender = studentDTO.getGender();
        String mobile = studentDTO.getMobile();
        String password = studentDTO.getPassword();
        if (!validateAllParams(name, gender, mobile, password)) {
            return Response.failure(configReader.getExceptionMessage(ExceptionType.NOT_COMPLETE));
        }
        return Response.success(baseService.register(studentDTO));
    }

    /**
     * 学员报名，车型信息必填
     *
     * @param request request
     * @param applyDTO applyDTO
     * @return 学员报名，车型信息必填
     */
    @PostMapping(value = "/apply")
    public Response<Object> apply(HttpServletRequest request, @RequestBody ApplyDTO applyDTO) {
        if (!validateAllParams(applyDTO.getLearnType())) {
            return Response.failure(configReader.getExceptionMessage(ExceptionType.NOT_COMPLETE));
        }
        int studentId = getNonNullCorrespondingIdByRequest(request.getHeader(Constant.TOKEN), Audience.STUDENT);
        applyDTO.setStudentId(studentId);
        return Response.success(baseService.apply(applyDTO));
    }

    @PostMapping(value = "/suggest")
    public Response<Object> suggest(HttpServletRequest request, @RequestBody SuggestDTO suggestDTO) {
        String studentId = JsonWebToken.parseStudent(request.getHeader(Constant.TOKEN)).get(Constant.ID);
        suggestDTO.setStudentId(Integer.parseInt(studentId));
        return Response.success(baseService.suggest(suggestDTO));
    }

    @Override
    @PostMapping(value = "/login")
    public Response<Map<String, String>> login(@RequestBody StudentDTO studentDTO) {
        if (!validateAllParams(studentDTO.getMobile(), studentDTO.getPassword())) {
            return Response.failure(configReader.getExceptionMessage(ExceptionType.NOT_COMPLETE));
        }
        return Response.success(baseService.login(studentDTO));
    }

    @GetMapping(value = "/my-coach")
    public Response<Map<String, String>> myCoach(HttpServletRequest request) {
        int id = Integer.parseInt(request.getHeader(Constant.TOKEN));
        return Response.success(baseService.myCoach(id));
    }

    /**
     * 推荐驾校
     *
     * @param areaName 区域名称
     * @return 推荐驾校
     */
    @GetMapping(value = "/school/list/{areaName}")
    public Response<Map<String, Object>> schoolListByAreaName(@PathVariable String areaName) {
        return Response.success(baseService.selectSchoolListByAreaName(areaName));
    }

    /**
     * 推荐教练
     *
     * @param areaName 区域名称
     * @return 推荐教练
     */
    @GetMapping(value = "/coach/list/{areaName}")
    public Response<Map<String, Object>> coachListByAreaName(@PathVariable String areaName) {
        return Response.success(baseService.selectCoachListByAreaName(areaName));
    }

    /**
     * 驾校详情
     *
     * @param schoolId schoolId
     * @return 驾校详情
     */
    @GetMapping(value = "/school/detail/{schoolId}")
    public Response<Map<String, Object>> schoolDetailByAreaName(@PathVariable int schoolId) {
        return Response.success(baseService.selectSchoolDetailBySchoolId(schoolId));
    }

    @GetMapping(value = "/pre-school")
    public Response<String> preSchool() {
        return Response.success(baseService.preSchool());
    }

    @GetMapping(value = "/post-school")
    public Response<String> postSchool() {
        return Response.success(baseService.postSchool());
    }

    /**
     * 记录日记，只需给detail即可
     *
     * <pre>
     * {
     *     "detail": "The Diary Detail."
     * }
     * </pre>
     *
     * @param request request
     * @param diary 日记详情，可能需要带格式
     * @return 记录日记
     */
    @PostMapping(value = "/record-diary")
    public Response<Map<String, Integer>> recordDiary(HttpServletRequest request, @RequestBody Diary diary) {
        int studentId = getNonNullCorrespondingIdByRequest(request.getHeader(Constant.TOKEN), Audience.STUDENT);
        diary.setStudentId(studentId);
        return Response.success(diaryService.recordDiary(diary));
    }

    /**
     * 日记点赞
     *
     * @param diaryId diaryId
     * @return 日记点赞
     */
    @GetMapping(value = "/diary/thumb-up/{diaryId}")
    public Response<Boolean> diaryThumbUp(@PathVariable int diaryId) {
        return Response.success(diaryService.diaryThumbUp(diaryId));
    }

    /**
     * 获取日志列表，默认分页大小为10，{@code URL: /diary/list/recordDateTime?currentPage=1}
     *
     * @param order 日记排序规则，精选为thumbUpCount，最新为recordDateTime
     * @param currentPage 如果小于0，默认为1
     * @return 获取日志列表
     */
    @GetMapping(value = "/diary/list/{order}")
    public Response<Map<String, Object>> diaryList(@PathVariable String order, @RequestParam int currentPage) {
        if (!StringUtils.equals(order, "thumbUpCount") && !StringUtils.equals(order, "recordDateTime")) {
            return Response.failure(configReader.getExceptionMessage(ExceptionType.NOT_COMPLETE));
        }
        if (currentPage <= 0) {
            currentPage = 1;
        }
        return Response.success(diaryService.selectDiaryByOrder(order, currentPage));
    }
}
